package net.java.mediasystems.database.core.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

@Entity
@Table(name = "STORAGE_CONTAINER")
public class StorageContainer implements Serializable {
	
	private static final long serialVersionUID = -2843335615119325608L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	private Long id;
	
	@Column(name = "NAME", nullable = false)
	private String name;
	
	@Column(name = "SIZE_IN_MB", nullable = false)
	private long sizeInMegaBytes;	// FIXME besser ist wohl sizeInGigaBytes!!!
	
	@OneToMany(mappedBy = "storageContainer")
	private List<MetadataValue> metadataValues;
	
	@Column(name = "INSERTED_DATE")
	private Date insertedDate;
	
	@Column(name = "UPDATED_DATE")
	private Date updatedDate;
	
	@PrePersist
	private void onCreate() {
		this.insertedDate = new Date();
	}
	
	@PreUpdate
	private void onUpdate() {
		this.updatedDate = new Date();
	}
	
	@Override
	public int hashCode() {
		final int prime = 43;
		int result = 1;
		
		result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		StorageContainer other = (StorageContainer) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getSizeInMegaBytes() {
		return sizeInMegaBytes;
	}

	public List<MetadataValue> getMetadataValues() {
		return metadataValues;
	}

	public void setMetadataValues(List<MetadataValue> metadataValues) {
		this.metadataValues = metadataValues;
	}

	public void setSizeInMegaBytes(long sizeInMegaBytes) {
		this.sizeInMegaBytes = sizeInMegaBytes;
	}

	public Date getInsertedDate() {
		return insertedDate;
	}

	public Date getUpdatedDate() {
		return updatedDate;
	}
}
